package com.person.monitor;

import android.content.Context;
import android.content.Intent;

import com.amap.api.location.CoordinateConverter;
import com.amap.api.location.DPoint;
import com.person.monitor.location.LocRet;

import java.util.List;

public class OpenWechatProcess extends BaseProcess {

    private static String keyValue = "Sky";

    private static LocRet workLoc = new LocRet(22.524674,113.939207,"work");//LocRet{lat=22.524674,lng=113.939207}

    private boolean needOpenWechat = true;

    public OpenWechatProcess(Context context, String name){
        super(context,name);
    }

    @Override
    public void processMonitor(List<MonitorInfo> list) {
        if(list.size() < 2 ) return;

        MonitorInfo now = list.get(0);

        if ((now.time > 1900 && now.time < 2300)
                || (now.time > 900 && now.time < 1000)) {
            if (isInCompany(now)) {
                if (needOpenWechat) {
                    openWechat();
                    needOpenWechat = false;
                }
            }
        } else {
            needOpenWechat = true;
        }
    }

    public static boolean isInCompany(MonitorInfo info){
        boolean ret = false;

        if(info.connectWifiSSID != null && info.connectWifiSSID.contains(keyValue)){
            ret = true;
        }

        if(info.locRet != null && calculateDistance(info.locRet) < 200){
            ret = true;
        }

        return ret;
    }

    private void openWechat(){
        LogcatHelper.getInstance().printLog("openWechat");

        Intent intent = new Intent();
        intent.setClassName("com.tencent.wework", "com.tencent.wework.launch.LaunchSplashActivity");
        intent.setFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY|Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            context.startActivity(intent);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static float calculateDistance(LocRet locRet){
        if(locRet == null) return 100000;
        DPoint dPoint = new DPoint(workLoc.lat, workLoc.lng);
        DPoint lPoint = new DPoint(locRet.lat, locRet.lng);
        float distance = CoordinateConverter.calculateLineDistance(dPoint,lPoint);
        return distance;
    }
}
